package photostock.model;

import java.io.File;
import java.util.Collections;
import java.util.Vector;

public class Album extends UserItem {
	
	public final static long serialVersionUID = 20090501L;
	
	private int id;
	
	public Album() {
		super(new User("shared"), "shared");
		neededFields = "id,nom,theme,skin,creationDate,user";
		childrenFields = "id,nom,partagee,validee,nbVisites,nbVotes,creationDate,album,user";
		children = new Vector<UserItem>();
		id = 0;
		initSharedItems();
	}
	
	
	public Album(UserItem user, String s) {
		super(user, s);
		neededFields = "id,nom,theme,skin,creationDate,user";
		childrenFields = "id,nom,partagee,validee,nbVisites,nbVotes,creationDate,album,user";
		children = new Vector<UserItem>();
		if (user.getChildrenItems().size() == 0) {
			id = 0;
			if (s.equals("shared")) initSharedItems();
		}
		else {
			Collections.sort(user.getChildrenItems(), getIdComparator());
			id = user.getChildrenItems().elementAt(user.getChildrenItems().size()-1).getId()+1;
		}
	}

	public String getType() {
		return (null == infos || !infos.exists() || null == infos.get("type") ? "" : infos.get("type"));
	}

	public void setType(String s) {
		if (null != infos && infos.exists() && null != infos.get("type")) infos.set("type", s);
	}

	
	public String getTheme() {
		return (null == infos || !infos.exists() || null == infos.get("theme") ? "default" : infos.get("theme"));
	}

	public void setTheme(String s) {
		if (null != infos && infos.exists() && null != infos.get("theme")) infos.set("theme", s);
	}

	public String getSkin() {
		return (null == infos || !infos.exists() || null == infos.get("skin") ? "default" : infos.get("skin"));
	}

	public void setSkin(String s) {
		if (null != infos && infos.exists() && null != infos.get("skin")) infos.set("skin", s);
	}

	public int getId() {
		return (null == infos || !infos.exists() || null == infos.get("id") ? id : Integer.parseInt(infos.get("id")));
	}

	public void setId(int id) {
		if (null != infos && infos.exists() && null != infos.get("id")) infos.set("id", new Integer(id).toString());
	}

	public String getCreationDate() {
		return (null == infos || !infos.exists() || null == infos.get("creationDate") ? "" : infos.get("creationDate"));
	}

	public void setCreationDate(String s) {
		if (null != infos && infos.exists() && null != infos.get("creationDate")) infos.set("creationDate", s);
	}

	
	private boolean initItem() {
		infos = super.initItem(this);
		return infos.exists();
	}

	public boolean init() {
		query = "SELECT * FROM album WHERE nom='"+getName()+"' and user='"+getParentItem().getName()+"'";
		return initItem();
	}

	public boolean createItem() {
		java.sql.Timestamp ts = new java.sql.Timestamp(new java.util.Date().getTime());
		query = "INSERT INTO album values("+id+", '" + getName() + "', '" + getTheme() + "', '" + getParentItem().getName() + "', '" + getSkin() + "', '" + ts.toString().substring(0, 19) + "')";
		return super.recItem(this);
	}
	
	public boolean createPhoto(File f) {
		boolean res = super.create(f);
		if (res) {
			Photo photo = new Photo(this, f.getName());
			photo.createItem();
			add(photo);
		}
		return res;
	}
	
	public boolean update() {
		query = "UPDATE album SET theme='" + getTheme() + "', skin='" + getSkin() + "' where nom='" + getName() + "' AND user = '" + getParentItem().getName() + "'";
		return super.updateItem(this);
	}
	
	public boolean delete() {
		query = "DELETE FROM album WHERE nom='" + getName() + "' AND user = '" + getParentItem().getName() + "'";
		return super.deleteItem(this);
	}
	
	public boolean initChildrenItems() {
		query = "SELECT * FROM photo WHERE album='" + getName() + "' AND user = '" + getParentItem().getName() + "' order by id";
		for (ItemInfo infos : super.getChildrenInfos()) {
			Photo child;
			if (!ItemFactory.isCached(infos.get("nom")+infos.get("id"))) {
				child = new Photo(this, infos.get("nom"));
			} else {
				child = (Photo)ItemFactory.getCached(infos.get("nom")+infos.get("id"));
				child.setParent(this);
			}
			child.init();
			add(child);
		}
		return children.size() == 0 ? false : true;
	}
	
	public boolean initSharedItems() {
		
		query = "SELECT * FROM photo WHERE partagee=" + new Boolean(true) + " AND validee = " + new Boolean(true) + " order by id";
		for (ItemInfo infos : super.getChildrenInfos()) {
			Photo child;
			if (!ItemFactory.isCached(infos.get("nom")+infos.get("id"))) {
				child = new Photo(infos.get("user"),infos.get("album"), infos.get("nom"));
				child.infos = infos;
				child.setParent(this);
			} else {
				child = (Photo)ItemFactory.getCached(infos.get("nom")+infos.get("id"));
				child.infos = infos;
				child.setParent(this);
			}
			child.initChildrenItems();
			add(child);
		}
		return children.size() == 0 ? false : true;
	}

}
